package com.hundun.hddeal.service.lmpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hundun.hddeal.dao.GzhMapper;
import com.hundun.hddeal.dao.NextOrderMapper;
import com.hundun.hddeal.entity.Gzh;
import com.hundun.hddeal.entity.GzhBody;
import com.hundun.hddeal.entity.NextOrder;
import com.hundun.hddeal.entity.NextOrderPuls;
import com.hundun.hddeal.response.Result;
import com.hundun.hddeal.response.ResultUtil;
import com.hundun.hddeal.service.NextOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 任务大厅小单业务层
 * @author 谢
 */
@Service
public class NextOrderServiceImpl implements NextOrderService {
    @Autowired
    NextOrderMapper nextOrderMapper;
    @Autowired
    GzhMapper gzhMapper;

    @Override
    public Result deleteByPrimaryKey(Long id) {
        return null;
    }

    @Override
    public Result insertSelective(NextOrder record) {
        try {
            nextOrderMapper.insertSelective(record);
            return ResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    @Override
    public Result<NextOrder> selectByPrimaryKey(Long id) {
        return null;
    }

    @Override
    public Result updateByPrimaryKeySelective(NextOrder record) {
        try{
            nextOrderMapper.updateByPrimaryKeySelective(record);
            return ResultUtil.success();
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    @Override
    public Result<PageInfo<NextOrderPuls>> selectGetofTask(String adId, String orderId, Integer page, Integer size,String type,Byte status,String gzhName,String order) {
        try {
            PageHelper.startPage(page, size);
            List<NextOrderPuls> orderList = nextOrderMapper.selectGetofTask(adId, orderId,type,status,gzhName,order);
            for (NextOrderPuls nextOrderPuls:orderList) {
                BigDecimal bg = new BigDecimal(nextOrderPuls.getSettMoney()+nextOrderPuls.getServeMoney());
                double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                nextOrderPuls.setRealityMoney(f1);
            }
            PageInfo<NextOrderPuls> pageInfo = new PageInfo<>(orderList);
            return ResultUtil.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    @Override
    public Result<PageInfo<NextOrder>> selectMtofTask(String mtId, Byte status, String gzhName,
                                                      Date startTime, Date endTime, Integer page, Integer size,String type,String hostId,String order) {
        try {
            PageHelper.startPage(page, size);
            List<NextOrder> orderList = nextOrderMapper.selectMtofTask(mtId, status, gzhName, startTime, endTime,type,hostId,order);
            for(NextOrder nextOrder:orderList){
                GzhBody gzh=gzhMapper.selectByPrimaryKey(nextOrder.getGhId());
                if("多图文头条".equals(nextOrder.getTgPlace())){
                    nextOrder.setYjRead(gzh.getReadnumAv());
                }else if("多图文次条".equals(nextOrder.getTgPlace())){
                    nextOrder.setYjRead(gzh.getNreadnumAv());
                }else {
                    nextOrder.setYjRead(gzh.getThreenumAv());
                }
            }
            PageInfo<NextOrder> pageInfo = new PageInfo<>(orderList);
            return ResultUtil.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    @Override
    public Result<PageInfo<NextOrder>> mtHostList(String mtId, String adName, Date startTime, Date endTime, Integer page, Integer size,String type) {
        try{
            List<Integer> intlist =new ArrayList<>();
            PageHelper.startPage(page, size);
            List<NextOrder> orderList = nextOrderMapper.mtHostList(mtId, adName, startTime,endTime,type);
            for(NextOrder nextOrder:orderList){
                List<NextOrder> list = nextOrderMapper.selectMtofTask(mtId, null, null, startTime, endTime,type,nextOrder.getHostId(),null);
                if("万粉".equals(type)){
                    switch (list.get(0).getStatus()){
                        case 0:
                            //审核中
                            nextOrder.setBigStatus(1);
                            break;
                        case 10:
                            //已完成
                            nextOrder.setBigStatus(2);
                            break;
                        default:
                            //审核通过
                            nextOrder.setBigStatus(3);
                            break;
                    }
                    for(NextOrder nextOrder1:list){
                       if(nextOrder1.getStatus()==7){
                           intlist.add(1);
                       }
                    }
                    if(intlist.size()==list.size()){
                        //审核不通过
                        nextOrder.setBigStatus(4);
                    }
                }else {
                    switch (list.get(0).getStatus()){
                        case 5:
                            //已完成
                            nextOrder.setBigStatus(1);
                            break;
                        case 0:
                            //审核中
                            nextOrder.setBigStatus(3);
                            break;
                        default:
                            //推广中
                            nextOrder.setBigStatus(2);
                            break;
                    }
                }

            }
            PageInfo<NextOrder> pageInfo = new PageInfo<>(orderList);
            return ResultUtil.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }
}